package org.leetcode.middle.leetcode155;



public class MinStack {

    //虚拟节点
    Node root;
    public MinStack() {
        this.root=new Node();
    }

    public void push(int val) {
        Node node = new Node(val);
        node.next=root.next;
        node.min= root.min;
        root.min=Math.min(node.min, val);
        root.next=node;
    }

    public void pop() {
        Node current = root.next;

        if (current.val==root.min){
            root.min= current.min;
        }
        root.next=current.next;
        current.next=null;
    }

    public int top() {
        return root.next.val;
    }

    public int getMin() {

        return root.min;
    }

    class Node{

       public int min;
        public  int val;
        public  Node next;

        public Node(int val,Node next){
            this.val=val;
            this.next=next;
        }

        public Node(int val){
            this.val=val;
        }

        public Node(){
            this.min=Integer.MAX_VALUE;
        }

    }

}
